import pandas as pd
from datetime import datetime, timedelta

from util.config_reader import get_root_path
from util.loguru_util import myLogger
import os

root_path = get_root_path()


def get_sheet_name():
    # 标志当前是采集第几个sheet页的数据
    timed_file_path = os.path.join(root_path, "timed.txt")
    if not os.path.exists(timed_file_path):
        open(timed_file_path, 'w', encoding='utf-8')

    with open(timed_file_path, 'r', encoding="utf-8") as rf:
        text = rf.readline()
    count = 1
    if text == "":
        with open(timed_file_path, "w", encoding='utf-8') as wf:
            wf.write(str(count))
    else:
        count = int(text)
        with open(timed_file_path, "w", encoding='utf-8') as wf:
            # if 0 < count <= 7:
            #     count += 1
            # else:
            #     count = 1
            count = count + 1 if 0 < count < 7 else 1
            wf.write(str(count))
    return str(count)


def readExcel(sheet_name: str = "0"):
    try:
        # strTime = (datetime.now() + timedelta(hours=170)).strftime("%Y-%m-%d")
        # sheet_name = get_sheet_name()
        myLogger.info(f"获取表中第{sheet_name} 个 sheet页的数据")
        df = pd.read_excel(os.path.join(root_path, "plan.xlsx"), sheet_name=sheet_name, engine='openpyxl')
        di = df.values.tolist()
        return di
    except Exception as e:
        myLogger.error(e)

# if __name__ == "__main__":
#     # readExcel()
#     for i in range(20):
#         # print(get_sheet_name())
#         readExcel(get_sheet_name())
